#include "udeletebookwidget.h"
#include "udispbookwidget.h"
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QMessageBox>
UDeleteBookWidget::UDeleteBookWidget(QWidget *parent)
	: QWidget(parent)
	, m_pISBN(new QLabel(this))
	, m_pTitle(new QLabel(this))
	, m_pISBNEdit(new QLineEdit(this))
	, m_pTitleEdit(new QLineEdit(this))
	, m_pQueryButton(new QPushButton(this))
	, m_pClearButton(new QPushButton(this))
	, m_pDeleteButton(new QPushButton(this))
	, m_pDispBookWidget(new UDispBookWidget(this))
{
	setWindowTitle("删除书籍");
	setAttribute(Qt::WA_StyledBackground, true);
	setStyleSheet(R"(UQueryBookWidget{background-color:#F5F5F5;border-radius:4px;border:1px solid #D9D8D7;border:none;})");
	resize(680, 500);
	m_pDispBookWidget->move(15, 90);

	initLabel();
	initLineEdit();
	initButton();
	connect(m_pQueryButton, &QPushButton::released, this, &UDeleteBookWidget::onQueryButtonReleased);
	connect(m_pClearButton, &QPushButton::released, this, &UDeleteBookWidget::onClearButtonReleased);
	connect(m_pDeleteButton, &QPushButton::released, this, &UDeleteBookWidget::onDeleteButtonReleased);
}

UDeleteBookWidget::~UDeleteBookWidget()
{}

void UDeleteBookWidget::onDeleteButtonReleased()
{
	bool empty = m_pDispBookWidget->m_pBooksInfoList.empty();
	QString str;
	for (UBooksInfo f : m_pDispBookWidget->m_pBooksInfoList) //后续可以执行批量删除处理
		str = f.m_isbn;
	if (empty == true) //未指定删除的数据
		return;
	else
	{
		QMessageBox msgBox;
		msgBox.setText("确定要删除吗？");
		msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel); // 设置标准按钮为“确认”和“取消”  
		msgBox.setDefaultButton(QMessageBox::Cancel);
		int ret = msgBox.exec();

		// 检查用户点击了哪个按钮  
		if (ret == QMessageBox::Ok)
		{
			UClientServer::Instance()->deleteBook(str);
			m_pDispBookWidget->clearWidget();
		}
	}
}

void UDeleteBookWidget::onQueryButtonReleased()
{
	m_pDispBookWidget->clearWidget();
	if (!m_pISBNEdit->text().isEmpty() || !m_pTitleEdit->text().isEmpty())
		UClientServer::Instance()->queryBook(m_pTitleEdit->text(), m_pISBNEdit->text(),"");
}

void UDeleteBookWidget::onClearButtonReleased()
{
	m_pDispBookWidget->clearWidget();
	m_pISBNEdit->clear();
	m_pTitleEdit->clear();
}

void UDeleteBookWidget::initButton()
{
	QString ButtonStyleSheet = R"(QPushButton{background-color:#808080;border:none;font:#000000;font-family:微软雅黑;font-size: 12px;}
									QPushButton:hover{background-color:#E9E9E9;})";
	m_pQueryButton->resize(60, 25);
	m_pQueryButton->move(430, 55);
	m_pQueryButton->setText("查询");
	m_pQueryButton->setStyleSheet(ButtonStyleSheet);

	m_pClearButton->resize(60, 25);
	m_pClearButton->move(500, 55);
	m_pClearButton->setText("清空");
	m_pClearButton->setStyleSheet(ButtonStyleSheet);

	m_pDeleteButton->resize(60, 30);
	m_pDeleteButton->move(140, 53);
	m_pDeleteButton->setText("删除");
	m_pDeleteButton->setStyleSheet(R"(QPushButton{background-color:#808080;border:none;font:#000000;font-family:微软雅黑;font-size: 12px;}
									QPushButton:hover{background-color:#DE325B;})");
}

void UDeleteBookWidget::initLabel()
{
	QString LabelStyleSheet = R"(QLabel{border:none;font:#000000;font-family:微软雅黑;font-size:15px;})";
	m_pTitle->resize(60, 25);
	m_pTitle->move(15, 15);
	m_pTitle->setText("书名:");
	m_pTitle->setStyleSheet(LabelStyleSheet);

	m_pISBN->resize(60, 25);
	m_pISBN->move(350, 15);
	m_pISBN->setText("ISBN:");
	m_pISBN->setStyleSheet(LabelStyleSheet);
}

void UDeleteBookWidget::initLineEdit()
{
	QString LineEditStyleSheet = R"(QLineEdit{background-color:#FFFFFF;border:none;font:#000000;font-family:微软雅黑;font-size:14px;})";
	m_pTitleEdit->resize(220, 25);
	m_pTitleEdit->move(75, 15);
	m_pTitleEdit->setStyleSheet(LineEditStyleSheet);

	m_pISBNEdit->resize(220, 25);
	m_pISBNEdit->move(410, 15);
	m_pISBNEdit->setStyleSheet(LineEditStyleSheet);

}

